/*	kd:
	This class handles cycling through a broken line message in a looping
	manner and draws it to the screen. The helper struct below it makes sure
	that only the necessary lines are in the line array. */

struct Gc_ScrollMsg play {
	Gc_MusicMenu m_menu;
	
	int   m_break_width;
	// Font  m_font;
	int   m_top_color;
	int   m_bottom_color;
	float m_y_offset;
	float m_x, m_y;
	float m_prev_offset;
	float m_line_spacing;
	
	ui void Draw (RenderEvent event, Font ffont, float scale = 2) const {
		float y_off =
			event.fractic * m_menu.m_unit_offset +
			(1 - event.fractic) * m_prev_offset;
		uint lines = m_menu.m_shown_count;
		
		float height   = 720 / scale;
		float width    = height * Screen.GetAspectRatio();
		
		for(uint i = 0; i < lines; i++) {
			float x    = m_x * width;
			float y    = m_y * height + (i + y_off) * m_line_spacing;
			
			Screen.DrawText(ffont, m_top_color, x, y,
				m_menu.m_shown_lines [i],
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_KEEPRATIO, true);
			Screen.DrawText(ffont, m_bottom_color, x, y,
				m_menu.m_shown_lines [i],
				DTA_ALPHA, i * 1.0 / (lines - 1),
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_KEEPRATIO, true);
		}
	}
	
	void UpdateScrolling () {
		m_prev_offset = m_menu.m_unit_offset;
		m_menu.UpdateScrolling();
	}
	
	void Init (
	int		break_width,
	double	x,
	double	y,
	Font	ffont,
	int		top_color = Font.CR_RED,
	int		bottom_color = Font.CR_YELLOW,
	double	line_spacing = (9)) {
		if(!ffont) {
			return;
		}
		
		if(break_width == 0) {
			break_width = 0x7ffff666;
		}
		
		m_break_width    = break_width;
		m_top_color      = top_color;
		m_bottom_color   = bottom_color;
		m_y_offset       = ffont.GetHeight();
		// m_font           = ffont;
		m_x              = x;
		m_y              = y;
		m_line_spacing   = line_spacing;
		// m_line_spacing   = ffont.GetHeight();
	}
	
	void SetColors (int top_color, int bottom_color) {
		m_top_color = top_color;
		m_bottom_color = bottom_color;
	}
	
	void Show (String text, Font ffont, uint to_show = 9) {
		BrokenLines lines = ffont.BreakLines(text,
			m_break_width == 0 ? 0x7ffff666 : m_break_width);
		m_menu.Show(lines, to_show);
	}
}

/*	kd:
	This handles scrolling through a menu of broken line options. I left this
	as-is, with no drawing and no specials, so client code can do cooler things
	with it.
	
	It's named Music Menu, because I took the idea from Touhou music menus. I
	recorded how they work: A certain count of items is displayed. When
	scrolling, one disappears and the next one is pushed in. It's too fast
	to see, so it looks pretty good, actually. */

struct Gc_MusicMenu {
	/*	Same dumb stuff here as in the type writer message. gzd for some reason
		allows you to make BrokenLines a member, but only when you load a
		savegame does it think it's a problem.
		
		I love this ingenious design! Tell me when it's too late! Great idea!
		Kind of like somebody else I know! */
	// BrokenLines    m_broken_lines;
	Array <String> m_lines;
	uint           m_broken_size;
	
	enum msg_state {
		MSG_STAY,
		MSG_DOWN,
		MSG_UP
	}
	
	float m_unit_offset;
	float m_offset_per_tic;
	float m_line_height;
	uint  m_shown_start_i;
	uint  m_shown_count;
	uint  m_scroll_time;
	uint  m_state_tics;
	msg_state m_state;
	// Font m_font;
	
	// kd:
	// This will have m_shown_count lines at all times. The trick is, you cannot
	// scroll ahead while the message is scrolling, because you can only scroll
	// per line. This makes it a bit tighter, much like a typical menu.
	Array <String> m_shown_lines;
	uint  m_first_i;
	
	void Show (BrokenLines lines, uint to_show) {
		if(!lines) {
			return;
		}
		
		m_state           = MSG_STAY;
		m_state_tics      = 0;
		m_scroll_time     = 0;
		m_offset_per_tic  = 0;
		m_unit_offset     = 0;
		m_first_i         = 0;
		m_broken_size     = lines.Count();
		m_shown_lines.Clear();
		
		if(0 < m_broken_size) {
			m_shown_count    = to_show;
			// m_broken_lines   = lines;
			
			for(uint i = lines.Count(); 0 <= --i;) {
				m_lines.Insert(0, lines.StringAt(i));
			}
			
			InitShownLines();
		}
		
		else {
			m_shown_count  = 0;
			m_lines.Clear();
		}
	}
	
	// The first time we build the shown lines array, it's kinda expensive like
	// this, but since we only scroll one line each time, every time we scroll,
	// we do something cooler.
	void InitShownLines () {
		if(m_broken_size == 0) {
			return;
		}
		
		m_shown_lines.Reserve(m_shown_count);
		uint cur_i = m_first_i % m_broken_size; // I LIKE MY RICE WITH CURI
		
		// Enter messages in a loop.
		for(int i = 0; i < m_shown_count; i++) {
			m_shown_lines [i] = m_lines [cur_i++];
			
			if(m_broken_size <= cur_i) {
				cur_i -= m_broken_size;
			}
		}
	}
	
	// Scrolls down by one line if not already scrolling.
	void ScrollDown (uint tics) {
		if(m_state != MSG_STAY || m_broken_size == 0) {
			return;
		}
		
		tics = max(1, tics);
		
		m_state            = MSG_DOWN;
		m_unit_offset      = 1;
		m_scroll_time      = tics;
		m_state_tics       = 0;
		m_offset_per_tic   = 1.0 / tics;
		m_first_i++;
		
		if(m_broken_size <= m_first_i) {
			m_first_i -= m_broken_size;
		}
		
		// We can take the array almost as-is. Almost.
		if(0 < m_shown_count) {
			m_shown_lines.Delete(0);
			m_shown_lines.Push(
				m_lines [(m_first_i + m_shown_count - 1) % m_broken_size] );
		}
	}
	
	// Scrolls up by one line if not already scrolling.
	void ScrollUp (uint tics) {
		if(m_state != MSG_STAY || m_broken_size == 0) {
			return;
		}
		
		tics = max(1, tics);
		
		m_state            = MSG_UP;
		m_unit_offset      = -1;
		m_scroll_time      = tics;
		m_state_tics       = 0;
		m_offset_per_tic   = 1.0 / tics;
		m_first_i--;
		
		if(m_first_i < 0) {
			m_first_i += m_broken_size;
		}
		
		// We can take the array almost as-is. Almost.
		if(0 < m_shown_count) {
			m_shown_lines.Delete(m_shown_count - 1);
			m_shown_lines.Insert(0, m_lines [m_first_i]);
		}
	}
	
	// Handle the real-time expensive graphicy scrolling business.
	void UpdateScrolling () {
		switch(m_state) {
		case MSG_DOWN:
			m_state_tics++;
			m_unit_offset -= m_offset_per_tic;
			
			if(m_scroll_time <= m_state_tics) {
				m_unit_offset = 0;
				m_state_tics  = 0;
				m_state       = MSG_STAY;
			}
			break;
		
		case MSG_UP:
			m_state_tics++;
			m_unit_offset += m_offset_per_tic;
			
			if(m_scroll_time <= m_state_tics) {
				m_unit_offset = 0;
				m_state_tics  = 0;
				m_state       = MSG_STAY;
			}
			break;
		}
	}
}